/**
 *Project: jh-plugins
 *File: cn.xkshow.plugins.page.PageTag.java 
 *Author: hardy(admin@xkshow.cn) Copyright
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-9-11         hardy         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.plugins.page;

import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.log4j.Logger;

import cn.xkshow.plugins.util.StringHelper;

public class PageTag extends SimpleTagSupport  {
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	private Page value;
	private String styleClass = "";//"spanStyle" or "spanStyle, this page A Label Style"
	private String javaScript = "";	
	private int pageStep = 6;//Page tab shows the number of
	private String viewData = "";//null|all|page|data,left|right
	
	public void doTag(){
		try {
			// <span>
			StringBuffer sburl = new StringBuffer("<span");
			if(StringHelper.isNotNull(this.styleClass)){
				sburl.append(" class='"+ this.styleClass.split(",")[0] +"'");
			}
			sburl.append(">");
			
			//page info
			if(null != this.value){				
				if(this.value.getCpage() > this.value.getTpage()){
					this.value.setCpage(this.value.getTpage());
				}
				
				//view pages tag
				int cPTagStepLcIndex = 0;
				int cPTagStep = (int) Math.ceil((this.value.getCpage() + pageStep - 1) / pageStep);
				int allPTagStep = (int) Math.ceil((this.value.getTpage() + pageStep - 1) / pageStep);
				if (this.value.getTpage() > 0) {
					if (cPTagStep != allPTagStep) {
						cPTagStepLcIndex = pageStep;
					} else {
						cPTagStepLcIndex = this.value.getTpage() % pageStep;
						if (cPTagStepLcIndex == 0)
							cPTagStepLcIndex = pageStep;
					}
				}
				
				this.viewData(sburl);
				//<a href='url||javascript:void(0);' onclick='pageOpen();' style='' class=''>1</a>
				if(StringHelper.isNotNull(this.javaScript)){
					pageLinkJs(sburl, new int[]{cPTagStepLcIndex, cPTagStep, allPTagStep});
				}else{
					//filename: url + dtotal=totalNum&tpage=pageTotalNum&cpage=curPageNum
					StringBuffer filename = new StringBuffer(null==this.value.getPgurl()?"":this.value.getPgurl());
					if (filename.toString().indexOf("?") == -1) {
						filename.append("?");
					} else if (!filename.toString().endsWith("&")) {
						filename.append("&");
					}
					filename.append("dtotal="+ this.value.getDtotal());
					filename.append("&cpage=");
					
					this.pageLinkHref(sburl, filename.toString(), new int[]{cPTagStepLcIndex, cPTagStep, allPTagStep});
				}	
			}
			
			// </span>
			sburl.append("</span>");
			super.getJspContext().getOut().print(sburl.toString());	
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("PageTag:"+ e);
		}		
    }
	
	//Href
	private String pageLinkHref(StringBuffer sburl, String filename, int aryPageSetp[]){
		if(this.value.getCpage() > 1){
			sburl.append("<a href='"+filename.toString()+"1'>|&lt;</a>");//Home
			sburl.append("<a href='"+filename.toString()+(this.value.getCpage()>1?this.value.getCpage()-1:1)+"'>&lt;&lt;</a>");//Back
		}		
		if(aryPageSetp[1] > 1){
			sburl.append("<a href='javascript:void(0);'>...</a>");//...
		}
		
		int pagenum = 1;
		for (int i = 0; i < aryPageSetp[0]; i++) {
			pagenum = i+1+(aryPageSetp[1]-1)*pageStep;
			sburl.append("<a href='"+ filename.toString()+pagenum +"'");			
			if(pagenum == this.value.getCpage()){
				if(StringHelper.isNotNull(this.styleClass) && this.styleClass.contains(",")){
					if(StringHelper.isNotNull(this.styleClass.split(",")[1])){
						sburl.append(" class='"+ this.styleClass.split(",")[1] +"'");
					}else{
						sburl.append(" style='color:#990000;font-weight:bold;'");
					}
				}else{
					sburl.append(" style='color:#990000;font-weight:bold;'");
				}				
			}
			sburl.append(">"+ pagenum +"</a>");
		}

		if(aryPageSetp[2]>1 && aryPageSetp[1]<aryPageSetp[2]){
			sburl.append("<a href='javascript:void(0);'>...</a>");//...
		}
		if(this.value.getCpage() < this.value.getTpage()){
			sburl.append("<a href='"+filename.toString()+(this.value.getCpage()<this.value.getTpage()?this.value.getCpage()+1:this.value.getTpage())+"'>&gt;&gt;</a>");//Next
			sburl.append("<a href='"+filename.toString()+this.value.getTpage()+"'>&gt;|</a>");//Last
		}
				
		return sburl.toString();
	}
	
	//javaScript
	private String pageLinkJs(StringBuffer sburl, int aryPageSetp[]){
		if(this.value.getCpage() > 1){
			sburl.append("<a href='"+ this.javaScript +"'>|&lt;</a>");//Home
			sburl.append("<a href='"+ this.javaScript +"'>&lt;&lt;</a>");//Back
		}
		if(aryPageSetp[1] > 1){
			sburl.append("<a href='javascript:void(0);'>...</a>");//...
		}
		
		int pagenum = 1;
		for (int i = 0; i <= aryPageSetp[0]; i++) {
			pagenum = i+1+(aryPageSetp[1]-1)*pageStep;
			sburl.append("<a href='"+ this.javaScript +"'");			
			if(pagenum == this.value.getCpage()){
				if(StringHelper.isNotNull(this.styleClass) && this.styleClass.contains(",")){
					if(StringHelper.isNotNull(this.styleClass.split(",")[1])){
						sburl.append(" class='"+ this.styleClass.split(",")[1] +"'");
					}else{
						sburl.append(" style='color:#990000;font-weight:bold;'");
					}
				}else{
					sburl.append(" style='color:#990000;font-weight:bold;'");
				}				
			}
			sburl.append(">"+ pagenum +"</a>");
		}
		
		if(aryPageSetp[1]>1 && aryPageSetp[1]<aryPageSetp[2]){
			sburl.append("<a href='javascript:void(0);'>...</a>");//...
		}
		if(this.value.getCpage() < this.value.getTpage()){
			sburl.append("<a href='"+ this.javaScript +"'>&gt;&gt;</a>");//Next
			sburl.append("<a href='"+ this.javaScript +"'>&gt;|</a>");//Last
		}
				
		return sburl.toString();
	}
	
	//view Data
	private String viewData(StringBuffer sburl){
		if(StringHelper.isNotNull(this.viewData) && this.viewData.length()>=3){
			String[] ary = this.viewData.split(",");
			if(null!=ary && StringHelper.isNotNull(ary[0])){
				sburl.append("<font style='float:"+ (ary.length>1?(StringHelper.isNotNull(ary[1])?ary[1]:""):"") +";'>");
				if(ary[0].equalsIgnoreCase("data")){
					sburl.append("共"+this.value.getDtotal()+"条数据");
				}else if(ary[0].equalsIgnoreCase("page")){
					sburl.append(this.value.getCpage()+"/"+this.value.getTpage()+"页");
				}else if(ary[0].equalsIgnoreCase("all")){
					sburl.append("共"+this.value.getDtotal()+"条数据&nbsp;&nbsp;"+this.value.getCpage()+"/"+this.value.getTpage()+"页");
				}
				sburl.append("</font>");
			}
		}
		
		return sburl.toString();
	}

	/*
	 * setter - getter
	 */
	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getJavaScript() {
		return javaScript;
	}

	public void setJavaScript(String javaScript) {
		this.javaScript = javaScript;
	}

	public int getPageStep() {
		return pageStep;
	}

	public Page getValue() {
		return value;
	}

	public void setValue(Page value) {
		this.value = value;
	}

	public void setPageStep(int pageStep) {
		this.pageStep = pageStep;
	}

	public String getViewData() {
		return viewData;
	}

	public void setViewData(String viewData) {
		this.viewData = viewData;
	}

	


}
